package co.sanduche.util;

import android.util.Log;

public abstract class DepVal2<D0, D1, T> extends DepVal<T> {

	private DepVal<D0> dep0;
	private DepVal<D1> dep1;
	private D0 d0;
	private D1 d1;

	public DepVal2(DepVal<D0> d0, DepVal<D1> d1) {
		dep0 = d0;
		dep1 = d1;
	}

	public abstract void asyncGetter(D0 d0, D1 d1);

	private synchronized void setDep0(D0 d0) {
		this.d0 = d0;
		startAsyncGetter();
	}

	private synchronized void setDep1(D1 d1) {
		this.d1 = d1;
		startAsyncGetter();
	}

	private void startAsyncGetter() {
		if (d0 != null && d1 != null) {

			asyncGetter(d0, d1);
		}
	}

	@Override
	protected void solveValue() {

		Log.e("DepVal2", "Va a obtener primera dependencia");
		dep0.onReady(new EPCallback<D0>() {

			@Override
			public void onSuccess(D0 arg0) {
				setDep0(arg0);
			}

			@Override
			public void onFailure(Throwable e) {
				failed(e);
			}
		});

		dep1.onReady(new EPCallback<D1>() {

			@Override
			public void onSuccess(D1 arg0) {
				setDep1(arg0);
			}

			@Override
			public void onFailure(Throwable e) {
				failed(e);
			}
		});

	}

}
